/*
 * @Author: szx
 * @Date: 2021-11-02 13:39:30
 * @LastEditTime: 2021-11-02 13:45:47
 * @Description:
 * @FilePath: \leetcode\200-299\203\203.cpp
 */

#include <iostream>
using namespace std;
struct ListNode {
    int val;
    ListNode* next;
    ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
   public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* prev = new ListNode(-1);
        prev->next = head;
        ListNode* cur = prev;
        while (cur != nullptr && cur->next != nullptr) {
            if (cur->next->val == val) {
                cur->next = cur->next->next;
            } else {
                cur = cur->next;
            }
        }
        return prev->next;
    }
};